import md5 from "md5";
import MessageObject from './messageObject';
export class ReceiverController {
    static instance = null;
    constructor(socket) {
        if (!ReceiverController.instance) {
            ReceiverController.instance = this;
            this.so = null;
            this.socket = socket;
            this.messageObject = new MessageObject();
        }
        return ReceiverController.instance
    }
    /*服务器收到消息*/
    receiverMsg(msg) {
        console.log("接受用户聊天消息时间:", msg);
        //1.收到消息发送回执
        if (!this.memberId) { console.log('未登录！memberId 为0！'); return { code: 301, data: '未登录！memberId 为0！' } }
        //3.各个消息类型处理
        this.messageObject[msg.head.action].getMsg(msg);
        //4.向服务器发送消息，通知收到消息的信息
        var serverMsg = { head: { po: 2, ms: 2, severMsgId: msg.head.serverMsgId, fromMemberId: this.memberId, action: '' }, body: { code: 200, data: '成功' } }
        const md5String = md5(JSON.stringify(serverMsg) + Date.now());
        serverMsg["head"]["clientMsgId"] = this.memberId + "_" + md5String;
        this.so.send(JSON.stringify(serverMsg));
        // console.log(serverMsg, this.memberId);
        return { code: 200, data: "成功！" }
    }
    /*接收member_id*/
    setMemberId(memberId) {
        this.memberId = memberId;
        return { code: 200, data: "成功！" };
    }
}